
/*
@file      2130.cpp
@brief     链表最大孪生和[https://leetcode.cn/problems/maximum-twin-sum-of-a-linked-list/description/]
@author    EricsT (EricsT@163.com)
@version   v1.0.0
@date      2025-10-14
@history   2025-10-14 EricsT - 新建文件
*/

#include "../Solution.h"


int Solution::pairSum(ListNode* head)
{
    int* ptr = new int[100000];//存储链表数据，假设大小为范围最大值

    int iSize = 0;//链表长度

    //链表数据全部存储在ptr内
    while (nullptr != head)
    {
        ptr[iSize] = head->val;
        
        head = head->next;
        iSize++;
    }

    int iMax = 0;//孪生和最大值

    //遍历算孪生和
    for (int i = 0; i < iSize / 2; ++i)
    {
        int val = ptr[i] + ptr[iSize - 1 - i];
        if (val > iMax)
            iMax = val;
    }

    delete[] ptr;
    return iMax;
}